<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-container>
        <el-header style="background-color: #0095d7">
            <h1 style="color: white;font-size: 22px">
                SuperMarket后台管理系统
                <span v-if="user.nickname!=null" style="float: right;font-size: 15px">欢迎{{user.nickname}}回来
                    <a href="javascript:void (0)" @click="logout()">退出登录</a>
                </span>
            </h1>
        </el-header>
        <el-container>
            <el-aside width="200px">
                <el-menu style="height: 800px"
                        @select="handleSelect"
                        default-active="2"
                        class="el-menu-vertical-demo"
                         unique-opened="true">
                    <el-submenu index="1">
                        <template slot="title">
                            <i class="el-icon-s-flag"></i>
                            <span>分类管理</span>
                        </template>
                        <el-menu-item index="1-1">分类列表</el-menu-item>
                        <el-menu-item index="1-2">添加分类</el-menu-item>
                    </el-submenu>

                    <el-submenu index="2">
                        <template slot="title">
                            <i class="el-icon-picture"></i>
                            <span>轮播图管理</span>
                        </template>
                        <el-menu-item index="2-1">轮播图列表</el-menu-item>
                        <el-menu-item index="2-2">添加轮播图</el-menu-item>
                    </el-submenu>

                    <el-submenu index="3">
                        <template slot="title">
                            <i class="el-icon-shopping-cart-2"></i>
                            <span>商品管理</span>
                        </template>
                        <el-menu-item index="3-1">商品列表</el-menu-item>
                        <el-menu-item index="3-2">添加商品</el-menu-item>
                    </el-submenu>

                    <el-submenu index="4">
                        <template slot="title">
                            <i class="el-icon-user"></i>
                            <span>用户信息管理</span>
                        </template>
                        <el-menu-item index="4-1">用户基本信息</el-menu-item>
                        <el-menu-item index="4-2">角色权限设置</el-menu-item>
                        <el-menu-item index="4-3">我的</el-menu-item>
                    </el-submenu>


                    <el-submenu index="5">
                        <template slot="title">
                            <i class="el-icon-s-marketing"></i>
                            <span>统计</span>
                        </template>
                        <el-menu-item index="5-1">用户消费记录</el-menu-item>
                        <el-menu-item index="5-2">用户充值记录</el-menu-item>

                    </el-submenu>


                </el-menu>
            </el-aside>







            <el-main>
                <el-table v-if="currentIndex=='1-1'"
                        :data="categoryArr"
                        style="width: 100%">
                    <!--type=index 设置这一列显示的内容为编号-->
                    <el-table-column type="index" label="编号"></el-table-column>
                    <el-table-column
                            prop="name"
                            label="分类名称"
                            width="180">
                    </el-table-column>
                    <el-table-column label="操作">
                        <!--scope里面装着当前行所对应的对象信息
                        scope.$index 得到当前行对应的对象在数组中的下标
                        scope.row 得到当前行对应的对象
                        -->
                        <template slot-scope="scope">
                            <el-button  size="mini" @click="categoryUpdate(scope.row)"
                                       type="success">修改</el-button>


                            <el-popconfirm
                                    title="您确定删除此分类吗？"
                                    @confirm="categoryDelete(scope.$index, scope.row)"
                            >
                                <el-button slot="reference" size="mini"
                                           type="danger">删除</el-button>
                            </el-popconfirm>


                        </template>
                    </el-table-column>
                </el-table>
                <el-table v-if="currentIndex=='2-1'"
                        :data="bannerArr"
                        style="width: 100%">
                    <!--type=index 设置这一列显示的内容为编号-->
                    <el-table-column type="index" label="编号"></el-table-column>
                    <el-table-column
                            label="轮播图"
                            width="180">
                        <template slot-scope="scope">
                            <!--scope.row代表当前行对应数组中的对象-->
                           <img :src="scope.row.url" width="100%">
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">

                        <template slot-scope="scope">
                            <el-button  size="mini" @click="bannerUpdate(scope.row)"
                                       type="success">修改</el-button>

                            <el-popconfirm
                                    title="您确定删除此轮播图吗？"
                                    @confirm="bannerDelete(scope.$index, scope.row)"
                            >
                                <el-button slot="reference" size="mini"
                                           type="danger">删除</el-button>
                            </el-popconfirm>


                        </template>
                    </el-table-column>
                </el-table>
                <el-table v-if="currentIndex=='3-1'"
                        :data="productArr"
                        style="width: 100%">
                    <!--type=index 设置这一列显示的内容为编号-->
                    <el-table-column type="index" label="编号"></el-table-column>
                    <el-table-column prop="title" label="商品标题" width="200"></el-table-column>
                    <el-table-column prop="price" label="价格" width="80"></el-table-column>
                    <el-table-column prop="oldPrice" label="原价" width="80"></el-table-column>
                    <el-table-column prop="saleCount" label="销量" width="80"></el-table-column>
                    <el-table-column
                            label="商品图片"
                            width="100">
                        <template slot-scope="scope">
                            <!--scope.row代表当前行对应数组中的对象-->
                            <img :src="scope.row.url" width="100%">
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button  size="mini" @click="productUpdate(scope.row)"
                                        type="success">修改</el-button>

                            <el-popconfirm
                                    title="您确定删除此商品吗？"
                                    @confirm="productDelete(scope.$index, scope.row)"
                            >
                                <el-button slot="reference" size="mini"
                                           type="danger">删除</el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>



                <el-table v-if="currentIndex=='4-1'"
                        :data="tableData"
                        style="width: 100%">
                    <!--type=index 设置这一列显示的内容为编号-->
                    <el-table-column type="index" label="编号" width="80"></el-table-column>
                    <el-table-column prop="date" label="注册时间" width="200"></el-table-column>
                    <el-table-column prop="name" label="姓名" width="150"></el-table-column>
                    <el-table-column prop="address" label="家庭住址" width="300"></el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button  size="mini" @click="productUpdate(scope.row)"
                                        type="success">修改</el-button>

                            <el-popconfirm
                                    title="您确定删除此用户信息吗？"
                                    @confirm="productDelete(scope.$index, scope.row)"
                            >
                                <el-button slot="reference" size="mini"
                                           type="danger">删除</el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>





                <template v-if="currentIndex=='4-2'">
                    <el-select v-model="value" placeholder="请选择">
                        <el-option
                                v-for="item in options"

                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                    <br>
                    <br>
                    <br>
                    <el-tree
                            :data="data"
                            show-checkbox
                            node-key="id"
                            :default-expanded-keys="[2, 3]"
                            :default-checked-keys="[5]"
                            :props="defaultProps">
                    </el-tree>



                </template>





                <!--统计-->
                <el-table v-if="currentIndex=='5-1'"
                          :data="moneyArr"
                          style="width: 100%">
                    <!--type=index 设置这一列显示的内容为编号-->
                    <el-table-column type="index" label="编号" width="100"></el-table-column>
                    <el-table-column prop="name" label="顾客姓名" width="100"></el-table-column>
                    <el-table-column prop="consume" label="日消费" width="100"></el-table-column>
                    <el-table-column prop="spends" label="月消费" width="80"></el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button  size="mini" @click="productUpdate(scope.row)"
                                        type="success">修改</el-button>

                            <el-popconfirm
                                    title="您确定删除此商品吗？"
                                    @confirm="productDelete(scope.$index, scope.row)"
                            >
                                <el-button slot="reference" size="mini"
                                           type="danger">删除</el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>


                <el-table v-if="currentIndex=='5-2'"
                          :data="rechargeArr"
                          style="width: 100%">
                    <!--type=index 设置这一列显示的内容为编号-->
                    <el-table-column type="index" label="编号" width="100"></el-table-column>
                    <el-table-column prop="name" label="顾客姓名" width="100"></el-table-column>
                    <el-table-column prop="recharge" label="充值" width="100"></el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button  size="mini" @click="productUpdate(scope.row)"
                                        type="success">修改</el-button>

                            <el-popconfirm
                                    title="您确定删除此商品吗？"
                                    @confirm="productDelete(scope.$index, scope.row)"
                            >
                                <el-button slot="reference" size="mini"
                                           type="danger">删除</el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>



            </el-main>
        </el-container>
    </el-container>
</div>
</body>




<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
   let v = new Vue({
        el: '#app',
        data: function() {
            return {
                data: [{
                    id: 1,
                    label: '分类管理',
                    children: [{
                        id: 6,
                        label: '二级 1-1',
                        children: [{
                            id: 11,
                            label: '三级 1-1-1'
                        }, {
                            id: 12,
                            label: '三级 1-1-2'
                        }]
                    }]
                },{
                    id: 2,
                    label: '轮播图管理',
                    children: [{
                        id: 7,
                        label: '二级 1-1',
                        children: [{
                            id: 13,
                            label: '三级 1-1-1'
                        }, {
                            id: 14,
                            label: '三级 1-1-2'
                        }]
                    }]
                },{
                    id: 3,
                    label: '商品管理',
                    children: [{
                        id: 8,
                        label: '二级 1-1',
                        children: [{
                            id: 15,
                            label: '三级 1-1-1'
                        }, {
                            id: 16,
                            label: '三级 1-1-2'
                        }]
                    }]
                },{
                    id: 4,
                    label: '用户信息管理',
                    children: [{
                        id: 9,
                        label: '二级 1-1',
                        children: [{
                            id: 17,
                            label: '三级 1-1-1'
                        }, {
                            id: 18,
                            label: '三级 1-1-2'
                        }]
                    }]
                },{
                    id: 5,
                    label: '统计',
                    children: [{
                        id: 10,
                        label: '二级 1-1',
                        children: [{
                            id: 19,
                            label: '三级 1-1-1'
                        }, {
                            id: 20,
                            label: '三级 1-1-2'
                        }]
                    }]
                } ],

                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
                options: [{
                    value: '选项1',
                    label: '店长'
                }, {
                    value: '选项2',
                    label: '主管'
                }, {
                    value: '选项3',
                    label: '区域经理'
                }, {
                    value: '选项4',
                    label: '普通员工'
                }, {
                    value: '选项5',
                    label: '用户'
                }],
                value: '',



                currentIndex:"1-1",
                bannerArr:[],
                categoryArr:[],
                productArr:[],
                user:{},
                personArr:[],
                moneyArr:[{
                    name:'王虎',
                    consume:100,
                    spends:2000,
                }],
                rechargeArr:[{
                    name:'王虎',
                    recharge:1000,
                }],
                customerArr:[{name:'王虎'},{name:'王小虎'}],

                tableData:[{
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                }],


            }
        },
       methods:{
           handleCommand(command) {
               v.$message(command);
               v.x=command;
           },
            logout(){
                if (confirm("您确定退出登录吗")){
                    axios.get("/logout").then(function () {
                        location.href="/";
                    })
                }

            },
           bannerUpdate(banner){
               location.href="/updateBanner.html?url="+banner.url+"&id="+banner.id;

           },
           bannerDelete(index,banner){
               axios.get("/banner/delete?id="+banner.id).then(function (response) {
                   v.bannerArr.splice(index,1);
               })
           },
           productUpdate(product){
             location.href="/updateProduct.html?id="+product.id;
           },
           productDelete(index,product){
               axios.get("/product/delete?id="+product.id).then(function (response) {
                   v.productArr.splice(index,1);
               })
           },
           categoryDelete(index,category){
               //发请求删除列表中的数据
               axios.get("/category/delete?id="+category.id).then(function (response) {
                  //删除数组中的元素数据(删除位置,删除数量)
                   v.categoryArr.splice(index,1);
               })
           },
           categoryUpdate(category){
               let name=prompt("请输入新的分类名称")
               if (name==null){
                   return;
               }
               if (name==''){
                   v.$message.error("分类名称不能为空")
                   return;
               }
               let c={id:category.id,name:name}
               axios.post("/category/update",c).then(function (response) {
                   category.name=name;
               })
           },

           handleSelect(key,keyPath){
               if (key=='1-2'){
                   let name=prompt("请输入分类的名称")
                   if (name==null){
                       return;
                   }
               if (name==''){
                       v.$message.error("分类名称不能为空")
                       return;
                   }
               axios.get("/category/insert?name="+name).then(function (response) {
                   //发送异步请求,将添加的分类信息请求回来,赋值给数组
                   v.categoryArr=response.data;
                   location.reload();
               })
               }else if (key=='2-2'){
                   location.href="insertBanner.html";/*跳转到轮播图添加页面*/
               }else if (key=='3-2'){
                   //跳转到添加商品页面
                location.href="insertProduct.html"
               }else if (key=='4-1'){
                   v.currentIndex=key;
               }
               else {
                   v.currentIndex=key;
               }
               v.currentIndex(key);
           },
           handleEdit(index, row) {
               console.log(index, row);
           },
           handleDelete(index, row) {
               console.log(index, row);
           },
       },
       created:function (){
            axios.get("/category/select").then(function (response) {
                v.categoryArr=response.data;
            })
           axios.get("/banner/select").then(function (response) {
               v.bannerArr=response.data;
           })
           axios.get("/product/select/admin").then(function (response) {
               v.productArr=response.data;
           })
           //发起请求获取当前登录用户信息 "欢迎xxx回来"
           axios.get("/currentUser").then(function (response) {
               v.user=response.data;
               if (v.user==""){
                   alert("请先登录")
                   location.href="/login.html";
               }
           })
       }
    })
</script>

</html>